import 'package:date_format/date_format.dart';

class TimeUtils {
  ///获取现在的时间
  static num getDayNow(){
    var nowTime = DateTime.now();
    return nowTime.millisecondsSinceEpoch;
  }

  ///获取格式化时间字符串，yyyy-mm-dd
  static String getATimeFormatToday(int millisecondsSinceEpoch){
    DateTime date = DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch);
    String timeFormatToday = formatDate(date, [yyyy,"-",mm,"-",dd,]);
    return timeFormatToday;
  }

  ///获取格式化时间字符串，yyyy-mm-dd HH:nn:ss
  static String getATimeFormatToMillSecond(int millisecondsSinceEpoch){
    DateTime date = DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch);
    String timeFormatToday = formatDate(date, [yyyy,"-",mm,"-",dd," ",HH,":",nn,":",ss]);
    return timeFormatToday;
  }

  ///获取今天的开始时间
  static int getDayBegin(){
    var nowTime = DateTime.now();
    var day =  DateTime(nowTime.year, nowTime.month, nowTime.day, 0,  0, 0);
    return day.millisecondsSinceEpoch;
  }

  ///获取昨天的开始时间
  static int getBeginDayOfYesterday(){
    var nowTime = DateTime.now();
    var yesterday = nowTime.add(const Duration(days: -1));
    var day =  DateTime(yesterday.year, yesterday.month, yesterday.day, 0,  0, 0);
    return day.millisecondsSinceEpoch;
  }

  ///获取昨天的结束时间
  static int getEndDayOfYesterDay(){
    var nowTime = DateTime.now();
    var yesterday = nowTime.add(const Duration(days: -1));
    var day =  DateTime(yesterday.year, yesterday.month, yesterday.day, 23,  59, 59);
    return day.millisecondsSinceEpoch;
  }

  ///获取本周的开始时间
  static int getBeginDayOfWeek(){
    var nowTime = DateTime.now();
    var weekday=nowTime.weekday;
    var yesterday = nowTime.add(Duration(days: -(weekday-1)));
    var day =  DateTime(yesterday.year, yesterday.month, yesterday.day, 0,  0, 0);
    return day.millisecondsSinceEpoch;
  }

  ///获取本月的开始时间
  static int getBeginDayOfMonth(){
    var nowTime = DateTime.now();
    var day =  DateTime(nowTime.year, nowTime.month, 1, 0,  0, 0);
    return day.millisecondsSinceEpoch;
  }

  ///获取本年的开始时间
  static int getBeginDayOfYear(){
    var nowTime = DateTime.now();
    var day =  DateTime(nowTime.year, 1, 1, 0,  0, 0);
    return day.millisecondsSinceEpoch;
  }

  ///根据时间字符串转时间戳
  static int getMillisecondsByFormatDate(String? date) {
    if (null == date) {
      return getDayNow().toInt();
    }
    //首先先将字符串格式的时间转为DateTime类型，因为millisecondsSinceEpoch方法转换要求为该格式
    var dateTime = DateTime.parse(date);
    var dateSeconds = dateTime .millisecondsSinceEpoch;
    return dateSeconds;

  }

  ///获取时区时间
  static String timezoneFormatToSecond(int millSecond,String timezoneLag){
    String str = TimeUtils.getATimeFormatToMillSecond(millSecond);
    String timeZone = timezoneLag;
    if(timeZone.isNotEmpty) {
      var second = millSecond;
      if(timeZone.startsWith("-")) {
        var lag = timeZone.replaceAll("-", "");
        second = millSecond - int.parse(lag) * 3600 * 1000;
      }else{
        var lag = timeZone.replaceAll("+", "");
        second = millSecond + int.parse(lag) * 3600 * 1000;
      }
      DateTime date = DateTime.fromMillisecondsSinceEpoch(second,isUtc: true);
      str = formatDate(date, [yyyy,"-",mm,"-",dd," ",HH,":",nn,":",ss]);

    }

    return str;
  }



}